package com.xinzhi.savingpot.task.service.impl;

import com.xinzhi.savingpot.task.entity.TaskInfo;
import com.xinzhi.savingpot.task.mapper.TaskCheckMapper;
import com.xinzhi.savingpot.task.service.ITaskCheckService;
import com.xinzhi.savingpot.task.vo.TaskCheckListVo;
import com.xinzhi.savingpot.util.BaseResponse;
import com.xinzhi.savingpot.util.ImageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class TaskCheckServiceImpl implements ITaskCheckService {
    @Autowired
    private TaskCheckMapper taskCheckMapper;
    @Autowired
    private ImageUtil imageUtil;
    @Override
    public BaseResponse getTaskCheckList(Integer stateID) throws IOException {
        if (stateID == null) return new BaseResponse(400,"查询失败");
        List<TaskInfo> taskCheckList;
        if (stateID == 1) {
            taskCheckList=taskCheckMapper.getTaskCheckList(1) ;
        } else if (stateID == 2) {
            taskCheckList = taskCheckMapper.getTaskCheckList(2);
        } else if (stateID == 3) {
            taskCheckList = taskCheckMapper.getTaskCheckList(6);
        } else {
            return new BaseResponse(400,"查询失败");
        }
        if (!taskCheckList.isEmpty()) {
            TaskCheckListVo tclv;
            List<TaskCheckListVo> tclvs = new ArrayList<>();
            for (TaskInfo ti: taskCheckList) {
                tclv = new TaskCheckListVo();
                tclv.setTaskID(ti.getTaskId());
                tclv.setUserID(ti.getUserInfo().getUserId());
                tclv.setReleaseTime(ti.getReleaseTime());
                tclv.setTaskTitle(ti.getTaskTitle());
                tclv.setUserheadphoto(imageUtil.image2Base64(ti.getUserInfo().getUserHeadPhoto()));
                tclv.setUserNickName(ti.getUserInfo().getUserNickName());
                tclvs.add(tclv);
            }
            return new BaseResponse(200,"查询成功", tclvs);
        } else {
            return new BaseResponse(400,"查询失败");
        }

    }
}
